<!DOCTYPE html><html lang="zh-CN" data-theme="dark"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Webpack4.0 | 北城韩雨</title><meta name="keywords" content="Webpack"><meta name="author" content="北城韩雨"><meta name="copyright" content="北城韩雨"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#0d0d0d"><meta name="description" content="Webpack4.0">
<meta property="og:type" content="article">
<meta property="og:title" content="Webpack4.0">
<meta property="og:url" content="http://example.com/2019/03/04/webpack4.0/index.html">
<meta property="og:site_name" content="北城韩雨">
<meta property="og:description" content="Webpack4.0">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2F3eca5394963b50e37b7be2ba320029ec%2F00003.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&refer=http%3A%2F%2Fpic.vjshi.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629530644&t=10679b5732c52e39eeb588040240a790">
<meta property="article:published_time" content="2019-03-03T16:00:00.000Z">
<meta property="article:modified_time" content="2019-06-02T16:00:00.000Z">
<meta property="article:author" content="北城韩雨">
<meta property="article:tag" content="Webpack">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2F3eca5394963b50e37b7be2ba320029ec%2F00003.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&refer=http%3A%2F%2Fpic.vjshi.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629530644&t=10679b5732c52e39eeb588040240a790"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="http://example.com/2019/03/04/webpack4.0/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'mediumZoom',
  Snackbar: undefined,
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isanchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'Webpack4.0',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2019-06-03 00:00:00'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
    const detectApple = () => {
      if (GLOBAL_CONFIG_SITE.isHome && /iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" href="/css/custom.css"  media="defer" onload="this.media='all'"><link rel="stylesheet" href="/css/coin.css"  media="defer" onload="this.media='all'"><link rel="stylesheet" href="/css/liuxing.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/l-lin/font-awesome-animation/dist/font-awesome-animation.min.css"  media="defer" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/swiper/swiper-bundle.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/zhheo/JS-Heo@main/bb/showbb_in_index.css"><!-- hexo injector head_end start --><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css" media="print" onload="this.media='screen'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/l-lin/font-awesome-animation/dist/font-awesome-animation.min.css" media="defer" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/hexo-butterfly-tag-plugins-plus/lib/tag_plugins.min.css" media="defer" onload="this.media='all'"><!-- hexo injector head_end end --><meta name="generator" content="Hexo 5.3.0"></head><body><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/img/my.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/archives/"><div class="headline">文章</div><div class="length-num">65</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/tags/"><div class="headline">标签</div><div class="length-num">23</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/categories/"><div class="headline">分类</div><div class="length-num">21</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/"><i class="fa-fw fas fa-home faa-tada"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/shuoshuo/"><i class="fa-fw fas fa-archive faa-tada"></i><span> 叨逼叨</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/tags/"><i class="fa-fw fas fa-tags faa-tada"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/categories/"><i class="fa-fw fas fa-folder-open faa-tada"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/link/"><i class="fa-fw fas fa-link faa-tada"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/comments/"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-message"></use></svg><span> 留言板</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/about/"><i class="fa-fw fas fa-heart faa-tada"></i><span> 个人简介</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2F3eca5394963b50e37b7be2ba320029ec%2F00003.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530644&amp;t=10679b5732c52e39eeb588040240a790')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">北城韩雨</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/"><i class="fa-fw fas fa-home faa-tada"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/shuoshuo/"><i class="fa-fw fas fa-archive faa-tada"></i><span> 叨逼叨</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/tags/"><i class="fa-fw fas fa-tags faa-tada"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/categories/"><i class="fa-fw fas fa-folder-open faa-tada"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/link/"><i class="fa-fw fas fa-link faa-tada"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/comments/"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-message"></use></svg><span> 留言板</span></a></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="/about/"><i class="fa-fw fas fa-heart faa-tada"></i><span> 个人简介</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">Webpack4.0</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2019-03-03T16:00:00.000Z" title="发表于 2019-03-04 00:00:00">2019-03-04</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2019-06-02T16:00:00.000Z" title="更新于 2019-06-03 00:00:00">2019-06-03</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/webpack/">webpack</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">7.3k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>28分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="Webpack4.0"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="webpack4-0"><a href="#webpack4-0" class="headerlink" title="webpack4.0"></a>webpack4.0</h1><h2 id="1-安装"><a href="#1-安装" class="headerlink" title="1.安装"></a>1.安装</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">webpack 安装首先需要安装两个东西</span><br><span class="line"><span class="number">1.</span>webpack 和 webpack-cli 工具  一般采用 -D(安装到开发依赖项目 等项目上线的时候就不需要了)</span><br></pre></td></tr></table></figure>
<h2 id="2-打包"><a href="#2-打包" class="headerlink" title="2.打包"></a>2.打包</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>用yarn init -y 初始化项目以后 然后我们就开始安装webpack 和webpack-cli等工具</span><br><span class="line"><span class="number">2.</span>打包启用命令 采用npx  webpack</span><br><span class="line"><span class="number">3.</span>这里启动了打包命令以后 他就会找打node_moudles下的bin文件  然后会找到webpck.cmd文件 里面是个判断语句</span><br></pre></td></tr></table></figure>
<h2 id="3-手动配置webpakc"><a href="#3-手动配置webpakc" class="headerlink" title="3.手动配置webpakc"></a>3.手动配置webpakc</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>刚刚我们所用的打包配置文件都是<span class="number">0</span>配置 比如打包之后的文件目录名称 和打包路径都是系统自动生成的</span><br><span class="line"><span class="number">2.</span>所以这里我们需要手动配置 默认配置文件的名字叫webpack.config.js</span><br></pre></td></tr></table></figure>
<h2 id="4-webpack-config-js基本配置"><a href="#4-webpack-config-js基本配置" class="headerlink" title="4.webpack.config.js基本配置"></a>4.webpack.config.js基本配置</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//webpack 是node 写出来的 所需遵循node 的模块化规范 也就是common.js规范 比如文件的导入 require 导出 module.exports</span></span><br><span class="line"><span class="keyword">let</span> path = <span class="built_in">require</span>(<span class="string">&#x27;path&#x27;</span>) <span class="comment">//node路径模块 这里要用因为是我们的出口文件 也就是打包之后的文件必须是绝对的路径</span></span><br><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">    mode:<span class="string">&#x27;development&#x27;</span> <span class="comment">//模式决定你是打包之后是生产还是上线 压缩代码的体积不一样</span></span><br><span class="line">    extry:<span class="string">&#x27;./src/index.js&#x27;</span><span class="comment">//你要从哪个文件打包</span></span><br><span class="line">    output:&#123;</span><br><span class="line">    filename:<span class="string">&#x27;bundle.js&#x27;</span><span class="comment">//你打包之后要输出的文件名称</span></span><br><span class="line">    path:path.resolve(__dirname,<span class="string">&#x27;build&#x27;</span>) <span class="comment">//要输出的路径和文件加名称 resolve这个方法就是帮你解决绝对路径的</span></span><br><span class="line">    publickPath:<span class="string">&#x27;http://www.baidu.com&#x27;</span> <span class="comment">//这个配置项目就是为我们所有打包的资源统一生成一个路径 后期你的资源可能都传送到cdn上 这样就可以直接写网路地址的路径了</span></span><br><span class="line">	<span class="comment">//比如我们文件的代码 &lt;img src=&#x27;./logo.png&#x27;&gt;  当我们加上了前缀之后 打包完毕之后 就是 http://www.baidu.com/logo.png了</span></span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="4-更改配置文件名称"><a href="#4-更改配置文件名称" class="headerlink" title="4.更改配置文件名称"></a>4.更改配置文件名称</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>我们默认的配置文件是webpack.confi.js 如果我们希望改名字怎么办 </span><br><span class="line"><span class="number">2.</span>在打包的时候就可以写npx webpack --config <span class="string">&#x27;你更改过后的配置文件名称&#x27;</span></span><br><span class="line"><span class="number">3.</span>也可以在你的package.json中添加脚本</span><br><span class="line">&#123;</span><br><span class="line">  <span class="string">&quot;name&quot;</span>: <span class="string">&quot;webpack&quot;</span>,</span><br><span class="line">  <span class="string">&quot;version&quot;</span>: <span class="string">&quot;1.0.0&quot;</span>,</span><br><span class="line">  <span class="string">&quot;main&quot;</span>: <span class="string">&quot;index.js&quot;</span>,</span><br><span class="line">  <span class="string">&quot;license&quot;</span>: <span class="string">&quot;MIT&quot;</span>,</span><br><span class="line">   <span class="comment">//下面就是你要添加的脚本</span></span><br><span class="line">  <span class="string">&quot;scripts&quot;</span>:&#123;</span><br><span class="line">    <span class="string">&quot;build&quot;</span>:<span class="string">&quot;webpack --config webpack.config.my.js&quot;</span></span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="string">&quot;devDependencies&quot;</span>: &#123;</span><br><span class="line">    <span class="string">&quot;webpack&quot;</span>: <span class="string">&quot;^4.41.5&quot;</span>,</span><br><span class="line">    <span class="string">&quot;webpack-cli&quot;</span>: <span class="string">&quot;^3.3.10&quot;</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="5-启动服务"><a href="#5-启动服务" class="headerlink" title="5.启动服务"></a>5.启动服务</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>我们传统的方式都是先打包 然后再新建一个html文件 去引入好你这个打包好的js文件 这样就十分的麻烦</span><br><span class="line">所以这里我们就会启动一个本地服务 所以就需要安装 webpack-dev-server的插件 这个时候你就可以再package.json里写你的启动脚本命令</span><br><span class="line">&#123;</span><br><span class="line">  <span class="string">&quot;name&quot;</span>: <span class="string">&quot;webpack&quot;</span>,</span><br><span class="line">  <span class="string">&quot;version&quot;</span>: <span class="string">&quot;1.0.0&quot;</span>,</span><br><span class="line">  <span class="string">&quot;main&quot;</span>: <span class="string">&quot;index.js&quot;</span>,</span><br><span class="line">  <span class="string">&quot;license&quot;</span>: <span class="string">&quot;MIT&quot;</span>,</span><br><span class="line">   <span class="comment">//下面就是你要添加的脚本</span></span><br><span class="line">  <span class="string">&quot;scripts&quot;</span>:&#123;</span><br><span class="line">    <span class="string">&quot;build&quot;</span>:<span class="string">&quot;webpack --config webpack.config.my.js&quot;</span></span><br><span class="line">     <span class="string">&quot;dev&quot;</span>:<span class="string">&quot;webpack-dev-server&quot;</span></span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="string">&quot;devDependencies&quot;</span>: &#123;</span><br><span class="line">    <span class="string">&quot;webpack&quot;</span>: <span class="string">&quot;^4.41.5&quot;</span>,</span><br><span class="line">    <span class="string">&quot;webpack-cli&quot;</span>: <span class="string">&quot;^3.3.10&quot;</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="number">2.</span>安装好了之后我们需要再我们的webpack.config.js里配置</span><br><span class="line"><span class="keyword">let</span> path = <span class="built_in">require</span>(<span class="string">&#x27;path&#x27;</span>) <span class="comment">//node路径模块 这里要用因为是我们的出口文件 也就是打包之后的文件必须是绝对的路径</span></span><br><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">    <span class="comment">//开发服务器的配置</span></span><br><span class="line">    devServer:&#123;</span><br><span class="line">        prot:<span class="number">3000</span>, <span class="comment">//服务启动的端口号</span></span><br><span class="line">        progress:<span class="literal">true</span>, <span class="comment">//进度条</span></span><br><span class="line">        contentBase:<span class="string">&#x27;./build&#x27;</span> <span class="comment">//你要读取哪个文件</span></span><br><span class="line">    &#125;</span><br><span class="line">    mode:<span class="string">&#x27;development&#x27;</span></span><br><span class="line">    extry:<span class="string">&#x27;./src/index.js&#x27;</span></span><br><span class="line">    output:&#123;</span><br><span class="line">    filename:<span class="string">&#x27;bundle.js&#x27;</span></span><br><span class="line">    path:path.resolve(__dirname,<span class="string">&#x27;build&#x27;</span>) </span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="number">3.</span>以上文件配置好了之后呢 我们只要西东就好了 他就会再文件里生成一个目录 但是我们没有html文件 所以这个时候我们就需要html-webpack-plugin 这个时候他会把你的html文件生成再内存中 不过前提是你要再你的src下设置一个模板</span><br><span class="line">下面是配置 首先还是要引入</span><br><span class="line"><span class="keyword">let</span> path = <span class="built_in">require</span>(<span class="string">&#x27;path&#x27;</span>)</span><br><span class="line"><span class="comment">//因为这里大写了 所以这是个构造函数也是es里的类 所以我们需要new出来</span></span><br><span class="line"><span class="keyword">let</span> HtmlWebpackPlugin = <span class="built_in">require</span>(<span class="string">&#x27;html-webpack-plugin&#x27;</span>)</span><br><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">    mode:<span class="string">&#x27;development&#x27;</span></span><br><span class="line">    extry:<span class="string">&#x27;./src/index.js&#x27;</span></span><br><span class="line">    output:&#123;</span><br><span class="line">    filename:<span class="string">&#x27;bundle.js&#x27;</span></span><br><span class="line">    path:path.resolve(__dirname,<span class="string">&#x27;build&#x27;</span>) </span><br><span class="line">	&#125;</span><br><span class="line">    <span class="comment">//这里方式所有的webpack插件</span></span><br><span class="line">    plguins:[</span><br><span class="line">        <span class="keyword">new</span> HtmlWebpackPlugin(&#123;</span><br><span class="line">            template:<span class="string">&#x27;./src/index.html&#x27;</span><span class="comment">//你模板的路径</span></span><br><span class="line">            filename:<span class="string">&#x27;index.html&#x27;</span><span class="comment">//你希望再内存路径中访问的文件地址</span></span><br><span class="line">            <span class="comment">//当我们打包以后 他会自动压缩你的js文件 不过也我们希望html文件也压缩 就需要下面的配置 比如你的html文件是双引号 之类的</span></span><br><span class="line">            minify:&#123;</span><br><span class="line">            <span class="comment">//去除双引号</span></span><br><span class="line">            	removeAttributeQuotes:<span class="literal">true</span>,</span><br><span class="line">            <span class="comment">//折叠成一行</span></span><br><span class="line">         		collapseWhitespace:<span class="literal">true</span></span><br><span class="line">        &#125;</span><br><span class="line">        &#125;)</span><br><span class="line">    ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="6-样式处理"><a href="#6-样式处理" class="headerlink" title="6.样式处理"></a>6.样式处理</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>首先我们本来样式的处理就是css文件 还有一些预编译的比如scss和less 遵循webpack的宗旨就是模块化 所以我们需要再配置文件里添加一个<span class="built_in">module</span> 模块的配置项里面是一些处理的规则rules:[]</span><br></pre></td></tr></table></figure>
<h2 id="7-处理js模块"><a href="#7-处理js模块" class="headerlink" title="7.处理js模块"></a>7.处理js模块</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>我们最常用的处理就是写的es6的语法 我们需要打包成es5 所以这个时候就需要用到babel的插件</span><br><span class="line"><span class="number">2.</span>安装 </span><br><span class="line">yarn add babel-loader @babel/core(核心模块) @babel/preset-env(转化模块)</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line">配置babel  因为是有loader所以需要在<span class="built_in">module</span>里的rules选项里去配置</span><br><span class="line"><span class="keyword">let</span> path = <span class="built_in">require</span>(<span class="string">&#x27;path&#x27;</span>)</span><br><span class="line"><span class="comment">//因为这里大写了 所以这是个构造函数也是es里的类 所以我们需要new出来</span></span><br><span class="line"><span class="keyword">let</span> HtmlWebpackPlugin = <span class="built_in">require</span>(<span class="string">&#x27;html-webpack-plugin&#x27;</span>)</span><br><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">    mode:<span class="string">&#x27;development&#x27;</span></span><br><span class="line">    extry:<span class="string">&#x27;./src/index.js&#x27;</span></span><br><span class="line">    output:&#123;</span><br><span class="line">    filename:<span class="string">&#x27;bundle.js&#x27;</span></span><br><span class="line">    path:path.resolve(__dirname,<span class="string">&#x27;build&#x27;</span>) </span><br><span class="line">	&#125;</span><br><span class="line">    <span class="built_in">module</span>:&#123;</span><br><span class="line">        rules:[</span><br><span class="line">            &#123;<span class="attr">test</span>:<span class="string">&#x27;/\.js$/&#x27;</span> <span class="comment">//正则 以.js结尾的都需要走我这个loader    </span></span><br><span class="line">             use:&#123;   <span class="comment">//使用什么样的loader去处理js文件</span></span><br><span class="line">             	loader:<span class="string">&#x27;babel-loader&#x27;</span>,</span><br><span class="line">             	options:&#123; <span class="comment">//配置选项 你要用那个模块去转换JS</span></span><br><span class="line">             		presets:[ <span class="comment">//预设 把js的语法转换 这个是大插件的集合 他可能自己有常用的插件 如果我们需要转换更高级别的语法 可能就要在配置一些小的插件 例如es7语法的class类</span></span><br><span class="line">             			<span class="string">&#x27;@babel/preset-env&#x27;</span></span><br><span class="line">             ],</span><br><span class="line">            		plugins:[</span><br><span class="line">                        <span class="string">&#x27;@babel/plugin-proposal-class-properties&#x27;</span></span><br><span class="line">                    ]</span><br><span class="line">            &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            &#125;, </span><br><span class="line">        ]</span><br><span class="line">        include:path.resolve(__dirname,<span class="string">&#x27;src&#x27;</span>) <span class="comment">//指让src下的JS文件去执行这个loader</span></span><br><span class="line">	    exclude:<span class="regexp">/node_modules/</span>  <span class="comment">//排除这个文件不执行这个loader</span></span><br><span class="line">    &#125;,</span><br><span class="line">    plguins:[</span><br><span class="line">        <span class="keyword">new</span> HtmlWebpackPlugin(&#123;</span><br><span class="line">            template:<span class="string">&#x27;./src/index.html&#x27;</span><span class="comment">//你模板的路径</span></span><br><span class="line">            filename:<span class="string">&#x27;index.html&#x27;</span><span class="comment">//你希望再内存路径中访问的文件地址</span></span><br><span class="line">            minify:&#123;</span><br><span class="line">            	removeAttributeQuotes:<span class="literal">true</span>,</span><br><span class="line">         		collapseWhitespace:<span class="literal">true</span></span><br><span class="line">        &#125;</span><br><span class="line">        &#125;)</span><br><span class="line">    ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>当我们用插件进行转换的时候 在转换的过程中 可能会用到向东的函数 这个时候 如果你有两个新的语法去转换那么这个时候就换打包转换的时候 会把这个公用的转换语法的方法 打包两次 还有一些系统内置的API</span><br><span class="line"><span class="number">2.</span>比如我们的<span class="class"><span class="keyword">class</span>类的语法 <span class="title">class</span> <span class="title">A</span></span>&#123;&#125; 和 <span class="class"><span class="keyword">class</span> <span class="title">B</span></span>&#123;&#125; 在转换的时候 都用了一个chencall的函数 那么这个时候这个函数就会生成两次 </span><br><span class="line"><span class="number">3.</span>使用@balbel/plugin-transform-runtime即可</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line">配置babel  因为是有loader所以需要在<span class="built_in">module</span>里的rules选项里去配置</span><br><span class="line"><span class="keyword">let</span> path = <span class="built_in">require</span>(<span class="string">&#x27;path&#x27;</span>)</span><br><span class="line"><span class="comment">//因为这里大写了 所以这是个构造函数也是es里的类 所以我们需要new出来</span></span><br><span class="line"><span class="keyword">let</span> HtmlWebpackPlugin = <span class="built_in">require</span>(<span class="string">&#x27;html-webpack-plugin&#x27;</span>)</span><br><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">    mode:<span class="string">&#x27;development&#x27;</span></span><br><span class="line">    extry:<span class="string">&#x27;./src/index.js&#x27;</span></span><br><span class="line">    output:&#123;</span><br><span class="line">    filename:<span class="string">&#x27;bundle.js&#x27;</span></span><br><span class="line">    path:path.resolve(__dirname,<span class="string">&#x27;build&#x27;</span>) </span><br><span class="line">	&#125;</span><br><span class="line">    <span class="built_in">module</span>:&#123;</span><br><span class="line">        rules:[</span><br><span class="line">            &#123;<span class="attr">test</span>:<span class="string">&#x27;/\.js$/&#x27;</span> <span class="comment">//正则 以.js结尾的都需要走我这个loader    </span></span><br><span class="line">             use:&#123;   <span class="comment">//使用什么样的loader去处理js文件</span></span><br><span class="line">             	loader:<span class="string">&#x27;babel-loader&#x27;</span>,</span><br><span class="line">             	options:&#123; <span class="comment">//配置选项 你要用那个模块去转换JS</span></span><br><span class="line">             		enforce:<span class="string">&#x27;pre&#x27;</span> <span class="comment">//强制最先执行我这个loader 不论书写顺序在哪里</span></span><br><span class="line">             		presets:[ <span class="comment">//预设 把js的语法转换 这个是大插件的集合 他可能自己有常用的插件 如果我们需要转换更高级别的语法 可能就要在配置一些小的插件 例如es7语法的class类</span></span><br><span class="line">             			<span class="string">&#x27;@babel/preset-env&#x27;</span></span><br><span class="line">             ],</span><br><span class="line">            		plugins:[</span><br><span class="line">                        <span class="string">&#x27;@babel/plugin-proposal-class-properties&#x27;</span> <span class="comment">//转换ES7class语法</span></span><br><span class="line">                        <span class="string">&#x27;@balbel/plugin-transform-runtime&#x27;</span> <span class="comment">//提取转换语法过程中的公共代码或者用到的相同的方法</span></span><br><span class="line">                    ]</span><br><span class="line">            &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            &#125;, </span><br><span class="line">        ]</span><br><span class="line">        include:path.resolve(__dirname,<span class="string">&#x27;src&#x27;</span>) <span class="comment">//指让src下的JS文件去执行这个loader</span></span><br><span class="line">	    exclude:<span class="regexp">/node_modules/</span>  <span class="comment">//排除这个文件不执行这个loader</span></span><br><span class="line">    &#125;,</span><br><span class="line">    plguins:[</span><br><span class="line">        <span class="keyword">new</span> HtmlWebpackPlugin(&#123;</span><br><span class="line">            template:<span class="string">&#x27;./src/index.html&#x27;</span><span class="comment">//你模板的路径</span></span><br><span class="line">            filename:<span class="string">&#x27;index.html&#x27;</span><span class="comment">//你希望再内存路径中访问的文件地址</span></span><br><span class="line">            <span class="comment">//模板压缩</span></span><br><span class="line">            minify:&#123;</span><br><span class="line">            	removeAttributeQuotes:<span class="literal">true</span>,</span><br><span class="line">         		collapseWhitespace:<span class="literal">true</span></span><br><span class="line">        &#125;</span><br><span class="line">        &#125;)</span><br><span class="line">    ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="8-语法校验"><a href="#8-语法校验" class="headerlink" title="8.语法校验"></a>8.语法校验</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>这里我们在校验的时候 也是通过laoder去实现的 所以还需要配置laoder(eslint-loader) 校验JS文件</span><br><span class="line"><span class="number">2.</span>安装 eslint eslint-loader</span><br><span class="line"><span class="number">3.</span>去下载校验的文件 官网 前面加.</span><br><span class="line"><span class="number">4.</span>laoder执行顺序 是从右向左 从下向上 有的时候 但是有的时候我们需要最先执行的loader却写在了上面 </span><br></pre></td></tr></table></figure>
<h2 id="9-全局变量引入"><a href="#9-全局变量引入" class="headerlink" title="9.全局变量引入"></a>9.全局变量引入</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>有的时候我们需要引入的模块比如jquey 挂在到我们的<span class="built_in">window</span>上</span><br><span class="line"><span class="number">2.</span>这个时候就需要安装expose-loader</span><br><span class="line"><span class="number">3.</span>有的时候我们引进的代码直接就是压缩完毕的 所以不需要他打包 这个时候就需要在<span class="keyword">export</span>.module =&#123;</span><br><span class="line">    externals:&#123;</span><br><span class="line">        jquery:<span class="string">&#x27;jQuery&#x27;</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="number">4.</span>读取文件的loader写法</span><br><span class="line">modules:&#123;</span><br><span class="line">    rules:[&#123;</span><br><span class="line">        <span class="comment">//只要我在文件使用导入了jquery 我就使用下面的laoder然后把他用$符号使用</span></span><br><span class="line">        test:<span class="built_in">require</span>.resolve(<span class="string">&#x27;jquery&#x27;</span>),</span><br><span class="line">        use:<span class="string">&#x27;expose-loader?$&#x27;</span></span><br><span class="line">    &#125;]</span><br><span class="line">&#125;</span><br><span class="line"><span class="number">5.</span>全局注入插件 在每个模块里注入一个你想要的东西 比如jquery</span><br><span class="line"><span class="comment">//这个插件就是在每个文件里 都注入了$ 来当jquery来用</span></span><br><span class="line"><span class="keyword">new</span> webpack.ProvidePlugin(&#123;</span><br><span class="line">    $:<span class="string">&#x27;jquery&#x27;</span></span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<h2 id="10-图片引入的问题"><a href="#10-图片引入的问题" class="headerlink" title="10.图片引入的问题"></a>10.图片引入的问题</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>首先图片我们引入的时候 可以使用 <span class="keyword">new</span> Image()这种</span><br><span class="line"><span class="number">2.</span>然后就是在css里 我们的background 的 url 也可以引入</span><br><span class="line"><span class="number">3.</span>然后是我们在html里引入 使用的&lt;img src=<span class="string">&#x27;./logo.png&#x27;</span>&gt; 在我们打包的时候 打包完成的图片会变成变成名字 所以 你html这样写了以后 就会找不到</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//new Image() </span></span><br><span class="line"><span class="keyword">import</span> img <span class="keyword">from</span> <span class="string">&#x27;./logo.png&#x27;</span></span><br><span class="line"><span class="keyword">const</span> imga = <span class="keyword">new</span> Image()</span><br><span class="line">imga.src = img</span><br><span class="line"><span class="comment">//url 样式里的</span></span><br><span class="line">body&#123;</span><br><span class="line">    background : url(<span class="string">&#x27;./logo.png&#x27;</span>)</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//以上两种 就可以用url loader 去解决</span></span><br><span class="line"><span class="built_in">module</span>:&#123;</span><br><span class="line">    rules:[&#123;</span><br><span class="line">        test:<span class="regexp">/\.(png|jpg|gif)$/</span>,</span><br><span class="line">        use:&#123;</span><br><span class="line">            laoder:<span class="string">&#x27;url-loader&#x27;</span>,</span><br><span class="line">            options:&#123;</span><br><span class="line">                limit: <span class="comment">//小于多少的时候 就变成base64</span></span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;]</span><br><span class="line">&#125;</span><br><span class="line">index 标签里的 就需要用到 html-withimg-loader 这个插件就是用来解析 html文件里的img图片路径问题</span><br><span class="line"><span class="built_in">module</span>:&#123;</span><br><span class="line">    rules:[&#123;</span><br><span class="line">        test:<span class="string">&#x27;/\.html$/&#x27;</span>,</span><br><span class="line">        use:<span class="string">&#x27;html-withimg-loader&#x27;</span></span><br><span class="line">    &#125;]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="10-loader的使用"><a href="#10-loader的使用" class="headerlink" title="10.loader的使用"></a>10.loader的使用</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>loader的分类</span><br><span class="line">pre loader 比如你的这个打包文件的规则需要再其他loader先执行 或者必须再某个指定的规则loader前面执行</span><br><span class="line">普通的loader 就是写好规则之后的那些 从右向左执行</span><br><span class="line">内连的loader 直接写再JS里 不需要通过webpack的配置文件去执行的</span><br><span class="line">比如 <span class="keyword">import</span> $ <span class="keyword">from</span> <span class="string">&#x27;expose-loader?$!jquery&#x27;</span> <span class="comment">//这个就是再单独文件里写把jquery暴漏出来 个￥$符号 挂在到了window上</span></span><br><span class="line">postloader 再其他loader后面执行的</span><br><span class="line">loader的执行顺序 是从右向左 从下向上的</span><br></pre></td></tr></table></figure>
<h2 id="11-创建分类"><a href="#11-创建分类" class="headerlink" title="11.创建分类"></a>11.创建分类</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>当我们不同类型的文件比如css 或者 图片 在打包的时候 希望他生成不同的文件目录</span><br><span class="line"><span class="number">2.</span>可以在每个loader里的options里 配置outpuPath:<span class="string">&#x27;目录名称&#x27;</span> 这里的outpuPath 就是生成一个文件目录夹</span><br><span class="line"><span class="number">3.</span>有的时候我们只需要我们的图片文件是cdn引入 所以就可以直接写publicPath</span><br><span class="line"><span class="built_in">module</span>:&#123;</span><br><span class="line">    rules:[</span><br><span class="line">        &#123;</span><br><span class="line">            test:<span class="string">&#x27;/\.(png|jpg|gif)$/&#x27;</span>,</span><br><span class="line">            use:&#123;</span><br><span class="line">                loader:<span class="string">&#x27;url-loader&#x27;</span>,</span><br><span class="line">                options:&#123;</span><br><span class="line">                    outputPath:<span class="string">&#x27;img/&#x27;</span>, <span class="comment">//打包之后的图片 生成在了 img文件下 当然在/后面可以写图片的名称之类的</span></span><br><span class="line">                    publicPath:<span class="string">&#x27;http://www.baidu.com&#x27;</span> </span><br><span class="line">                    <span class="comment">//&lt;img src=&quot;./logo.png&quot;&gt; 打包完毕之后 &lt;img src=&quot;http://www.baidu.com/logo.png&quot;&gt;</span></span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="12-打包多页面应用"><a href="#12-打包多页面应用" class="headerlink" title="12.打包多页面应用"></a>12.打包多页面应用</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>我们以往的都是打包单页面应用 只有一个js文件 然后webpack会从你这个入口文件里面去层层递归 最后打包</span><br><span class="line"><span class="number">2.</span>现在我们有两个入口的js文件 所以配置就需要配置两个入口 和两个出口文件</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line">配置</span><br><span class="line"><span class="keyword">let</span> path = <span class="built_in">require</span>(<span class="string">&#x27;path&#x27;</span>)</span><br><span class="line"><span class="keyword">let</span> HtmlWebpackPlugin = <span class="built_in">require</span>(<span class="string">&#x27;HtmlWebpackPlugin&#x27;</span>)</span><br><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">    mode:<span class="string">&#x27;development&#x27;</span>,</span><br><span class="line">    entry:&#123;</span><br><span class="line">        home:<span class="string">&#x27;./src/index.js&#x27;</span> <span class="comment">//主页的入口js文件</span></span><br><span class="line">        other:<span class="string">&#x27;./src/other.js&#x27;</span><span class="comment">//其他页的入口文件</span></span><br><span class="line">    &#125;,</span><br><span class="line">    output:&#123;</span><br><span class="line">        filename:<span class="string">&#x27;[name].js&#x27;</span> <span class="comment">//这里的name就是变量 他是你上面两个入口文件的名字 会自动执行</span></span><br><span class="line">        path:path.resolve(__dirname,<span class="string">&#x27;dist&#x27;</span>)</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="comment">//因为是两个页面 所以就需要new两次 </span></span><br><span class="line">    plugin:[</span><br><span class="line">        <span class="keyword">new</span> HtmlWebpackPlugin(&#123;</span><br><span class="line">            template:<span class="string">&#x27;./index.html&#x27;</span></span><br><span class="line">            filename:<span class="string">&#x27;home.html&#x27;</span></span><br><span class="line">            <span class="comment">//引入哪个js文件</span></span><br><span class="line">            chunks:[<span class="string">&#x27;home&#x27;</span>]</span><br><span class="line">        &#125;)</span><br><span class="line">        <span class="keyword">new</span> HtmlWebpackPlugin(&#123;</span><br><span class="line">            template:<span class="string">&#x27;./index.html&#x27;</span></span><br><span class="line">            filename:<span class="string">&#x27;other.html&#x27;</span></span><br><span class="line">             <span class="comment">//如果需要引入多个js文件</span></span><br><span class="line">            chunks:[<span class="string">&#x27;other&#x27;</span>,<span class="string">&#x27;home&#x27;</span>]</span><br><span class="line">        &#125;)</span><br><span class="line">    ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="13-配置source-map"><a href="#13-配置source-map" class="headerlink" title="13.配置source-map"></a>13.配置source-map</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>首先我们在打包的时候 代码都会被压缩 不方面我们查看错误</span><br><span class="line"><span class="number">2.</span>所以就需要把我们不打包的源码映射到内存中</span><br><span class="line"><span class="number">3.</span>devtool:<span class="string">&#x27;source-map&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> path = <span class="built_in">require</span>(<span class="string">&#x27;path&#x27;</span>)</span><br><span class="line"><span class="keyword">let</span> HtmlWebpackPlugin = <span class="built_in">require</span>(<span class="string">&#x27;HtmlWebpackPlugin&#x27;</span>)</span><br><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">    mode:<span class="string">&#x27;development&#x27;</span>,</span><br><span class="line">    entry:&#123;</span><br><span class="line">        home:<span class="string">&#x27;./src/index.js&#x27;</span> <span class="comment">//主页的入口js文件</span></span><br><span class="line">    &#125;,</span><br><span class="line">    output:&#123;</span><br><span class="line">        filename:<span class="string">&#x27;[name].js&#x27;</span> <span class="comment">//这里的name就是变量 他是你上面两个入口文件的名字 会自动执行</span></span><br><span class="line">        path:path.resolve(__dirname,<span class="string">&#x27;dist&#x27;</span>)</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="comment">//因为是两个页面 所以就需要new两次 </span></span><br><span class="line">    plugin:[</span><br><span class="line">        <span class="keyword">new</span> HtmlWebpackPlugin(&#123;</span><br><span class="line">            template:<span class="string">&#x27;./index.html&#x27;</span></span><br><span class="line">            filename:<span class="string">&#x27;home.html&#x27;</span></span><br><span class="line">            <span class="comment">//引入哪个js文件</span></span><br><span class="line">            chunks:[<span class="string">&#x27;home&#x27;</span>]</span><br><span class="line">        &#125;)</span><br><span class="line">    ],</span><br><span class="line">     devtool:<span class="string">&#x27;source-map&#x27;</span> <span class="comment">//把不打包的源码映射到内存中 方便查看错误 会单独生成一个很大的文件</span></span><br><span class="line">	 devtool:<span class="string">&#x27;eval-source-map&#x27;</span> <span class="comment">//也会映射 但是不会单独生成一个打文件</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="14-webpack跨域"><a href="#14-webpack跨域" class="headerlink" title="14.webpack跨域"></a>14.webpack跨域</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>我们前端的服务器代码 和我们后端的服务器代码肯定不一样 所以就会存在跨域</span><br><span class="line"><span class="number">2.</span>所以我们需要配置代理 在devServer 也就是我们的webpack-devserver的插件中去配置</span><br><span class="line"><span class="number">3.</span>我们后端服务器是自己写的 然后在后端打包前端的代码 也能访问到后端的地址</span><br><span class="line"><span class="keyword">let</span> path = <span class="built_in">require</span>(<span class="string">&#x27;path&#x27;</span>)</span><br><span class="line"><span class="keyword">let</span> HtmlWebpackPlugin = <span class="built_in">require</span>(<span class="string">&#x27;HtmlWebpackPlugin&#x27;</span>)</span><br><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">    mode:<span class="string">&#x27;development&#x27;</span>,</span><br><span class="line">    devServer:&#123;</span><br><span class="line">        <span class="comment">//这个意思就是凡是你以api(前提是后端给你写的接口是以api开头)开头的请求路径 就去这个端口请求</span></span><br><span class="line">        proxy:&#123;</span><br><span class="line">            <span class="string">&#x27;/api&#x27;</span>:<span class="string">&#x27;http://localhost:3000&#x27;</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    entry:&#123;</span><br><span class="line">        home:<span class="string">&#x27;./src/index.js&#x27;</span> <span class="comment">//主页的入口js文件</span></span><br><span class="line">    &#125;,</span><br><span class="line">    output:&#123;</span><br><span class="line">        filename:<span class="string">&#x27;[name].js&#x27;</span> <span class="comment">//这里的name就是变量 他是你上面两个入口文件的名字 会自动执行</span></span><br><span class="line">        path:path.resolve(__dirname,<span class="string">&#x27;dist&#x27;</span>)</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="comment">//因为是两个页面 所以就需要new两次 </span></span><br><span class="line">    plugin:[</span><br><span class="line">        <span class="keyword">new</span> HtmlWebpackPlugin(&#123;</span><br><span class="line">            template:<span class="string">&#x27;./index.html&#x27;</span></span><br><span class="line">            filename:<span class="string">&#x27;home.html&#x27;</span></span><br><span class="line">            <span class="comment">//引入哪个js文件</span></span><br><span class="line">            chunks:[<span class="string">&#x27;home&#x27;</span>]</span><br><span class="line">        &#125;)</span><br><span class="line">    ],</span><br><span class="line">	devtool:<span class="string">&#x27;eval-source-map&#x27;</span> <span class="comment">//也会映射 但是不会单独生成一个打文件</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>但是我们后端的接口可能是好几个人写的 所以不可能都给你写成api开头 这个时候就需要我们前端自己写 </span><br><span class="line"><span class="number">2.</span>前端在请求接口的时候都写成以api开头 然后再配置的时候 再把api去掉</span><br><span class="line"><span class="keyword">let</span> path = <span class="built_in">require</span>(<span class="string">&#x27;path&#x27;</span>)</span><br><span class="line"><span class="keyword">let</span> HtmlWebpackPlugin = <span class="built_in">require</span>(<span class="string">&#x27;HtmlWebpackPlugin&#x27;</span>)</span><br><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">    mode:<span class="string">&#x27;development&#x27;</span>,</span><br><span class="line">    devServer:&#123;</span><br><span class="line">        <span class="comment">//这个意思就是凡是你以api(前提是后端给你写的接口是以api开头)开头的请求路径 就去这个端口请求</span></span><br><span class="line">        proxy:&#123;</span><br><span class="line">            <span class="comment">//就是你前端请求的是以api开头的路径  然后去目标路径那去找 然后再把api去掉</span></span><br><span class="line">            <span class="string">&#x27;/api&#x27;</span>:&#123;</span><br><span class="line">                target:<span class="string">&#x27;http://localhost:3000&#x27;</span> <span class="comment">//最后请求的路径</span></span><br><span class="line">            	pathRewrite:&#123;<span class="string">&#x27;/api&#x27;</span>:<span class="string">&#x27;&#x27;</span>&#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    entry:&#123;</span><br><span class="line">        home:<span class="string">&#x27;./src/index.js&#x27;</span> <span class="comment">//主页的入口js文件</span></span><br><span class="line">    &#125;,</span><br><span class="line">    output:&#123;</span><br><span class="line">        filename:<span class="string">&#x27;[name].js&#x27;</span> <span class="comment">//这里的name就是变量 他是你上面两个入口文件的名字 会自动执行</span></span><br><span class="line">        path:path.resolve(__dirname,<span class="string">&#x27;dist&#x27;</span>)</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="comment">//因为是两个页面 所以就需要new两次 </span></span><br><span class="line">    plugin:[</span><br><span class="line">        <span class="keyword">new</span> HtmlWebpackPlugin(&#123;</span><br><span class="line">            template:<span class="string">&#x27;./index.html&#x27;</span></span><br><span class="line">            filename:<span class="string">&#x27;home.html&#x27;</span></span><br><span class="line">            <span class="comment">//引入哪个js文件</span></span><br><span class="line">            chunks:[<span class="string">&#x27;home&#x27;</span>]</span><br><span class="line">        &#125;)</span><br><span class="line">    ],</span><br><span class="line">	devtool:<span class="string">&#x27;eval-source-map&#x27;</span> <span class="comment">//也会映射 但是不会单独生成一个打文件</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//后端代码打包前端代码</span></span><br><span class="line"><span class="keyword">const</span> express = <span class="built_in">require</span>(<span class="string">&#x27;express&#x27;</span>)</span><br><span class="line"><span class="keyword">const</span> app = express()</span><br><span class="line"><span class="keyword">let</span> webpack = <span class="built_in">require</span>(<span class="string">&#x27;webpack&#x27;</span>)</span><br><span class="line"><span class="comment">//读取了前端的webpack配置</span></span><br><span class="line"><span class="keyword">let</span> config = <span class="built_in">require</span>(<span class="string">&#x27;./webpack.conf.js&#x27;</span>)</span><br><span class="line"><span class="comment">//中间件 </span></span><br><span class="line"><span class="keyword">let</span> middle = <span class="built_in">require</span>(<span class="string">&#x27;webpack-dev-middleware&#x27;</span>)</span><br><span class="line"><span class="comment">//可理解为打包了前端代码</span></span><br><span class="line"><span class="keyword">let</span> compiler = webpack(config)</span><br><span class="line"><span class="comment">//使用了代理中间件 完成跨域</span></span><br><span class="line">app.use(middle(compiler))</span><br><span class="line">app.get(<span class="string">&#x27;/user&#x27;</span>,<span class="function">(<span class="params">req,res</span>) =&gt;</span> &#123;</span><br><span class="line">    res.json(&#123;<span class="attr">name</span>:<span class="string">&#x27;1111&#x27;</span>&#125;)</span><br><span class="line">&#125;)</span><br><span class="line">app.listen(<span class="number">3000</span>)</span><br></pre></td></tr></table></figure>
<h2 id="15-resolve属性的配置"><a href="#15-resolve属性的配置" class="headerlink" title="15.resolve属性的配置"></a>15.resolve属性的配置</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>我们的的模块 是common.js规范的 再查找第三方包的时候 都去node_modules的文件去找json文件 然后找到main属性 如果当前路径下没有 就接着向上查找</span><br><span class="line"><span class="number">2.</span>这里就可以通过配置 resolve属性 去改变他的查找的机制</span><br><span class="line"></span><br><span class="line">resolve:&#123;</span><br><span class="line">    <span class="comment">//这里配置的意思就是指再当前目录下的node_modules里去找</span></span><br><span class="line">    modules:[path.resolve(<span class="string">&#x27;node_modules&#x27;</span>)]</span><br><span class="line">    <span class="comment">//这个选项就是我们再import 的时候 可以不用写文件的后缀</span></span><br><span class="line">    extensions: [<span class="string">&#x27;.js&#x27;</span>, <span class="string">&#x27;.vue&#x27;</span>, <span class="string">&#x27;.json&#x27;</span>],</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="16-定义环境变量"><a href="#16-定义环境变量" class="headerlink" title="16.定义环境变量"></a>16.定义环境变量</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>有的时候我们需要执行不同的webpack配置文件 比如你开发环境的时候访问后台的接口是一个地址 打包之后访问的接口又是另外一个地址</span><br><span class="line"><span class="number">2.</span>webpack自带的插件 叫webpack.DefindePlugin() 可以给我们的去插入一个全局的变量 我们就可以通过判断这个变量的值 来让他跑什么环境</span><br><span class="line">plugin:[</span><br><span class="line">    <span class="keyword">new</span> webpack.DefindePlugin(&#123;</span><br><span class="line">        <span class="comment">//这里就是再全局插入了一个变量</span></span><br><span class="line">        DEV:<span class="string">&#x27;JSON.stringify(&#x27;</span>dev<span class="string">&#x27;)&#x27;</span></span><br><span class="line">        BASE_URL:<span class="string">&#x27;http://localhost:3000&#x27;</span></span><br><span class="line">    &#125;)</span><br><span class="line">]</span><br><span class="line"><span class="comment">//通过判断这个变量来改变请求的路径</span></span><br><span class="line"><span class="keyword">let</span> url = <span class="string">&#x27;&#x27;</span></span><br><span class="line"><span class="keyword">if</span>(DEV === dev)&#123;</span><br><span class="line">    url = <span class="string">&#x27;http://localhost:3000&#x27;</span></span><br><span class="line">&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    url = <span class="string">&#x27;www.houtai.8080&#x27;</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="17-区分不同环境"><a href="#17-区分不同环境" class="headerlink" title="17.区分不同环境"></a>17.区分不同环境</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>通过以上我们就可以区分不同的环境从而做到不同的配置 但是我们首先要配置一个公共的配置文件 不管是何种环境下 这些公共配置文件都要用到的</span><br><span class="line"><span class="number">2.</span>通过webpack-merge 模块来合并公共的配置文件</span><br><span class="line"></span><br><span class="line"><span class="number">1</span>-开发配置文件 webpack.dev.js</span><br><span class="line"><span class="keyword">let</span> &#123;smart&#125; = <span class="built_in">require</span>(<span class="string">&#x27;webpack-merge&#x27;</span>) <span class="comment">//加载</span></span><br><span class="line"><span class="comment">//引入公共配置文件</span></span><br><span class="line"><span class="keyword">let</span> base = <span class="built_in">require</span>(<span class="string">&#x27;./webpack.base.js&#x27;</span>)</span><br><span class="line"><span class="comment">//配置一下开发环境的配置 比如可以devtool 调试之类的选项 显示合并公共文件的配置 然后再配置自己的</span></span><br><span class="line">modele.exports = smart(base,&#123;</span><br><span class="line">    mode:<span class="string">&#x27;deveploment&#x27;</span>,</span><br><span class="line">    devtool:<span class="string">&#x27;srouse-map&#x27;</span>,</span><br><span class="line">    plugin:[</span><br><span class="line">        <span class="keyword">new</span> webpack.DefindePlugin(&#123;</span><br><span class="line">        DEV:<span class="string">&#x27;JSON.stringify(&#x27;</span>dev<span class="string">&#x27;)&#x27;</span></span><br><span class="line">        BASE_URL:<span class="string">&#x27;http://localhost:3000&#x27;</span></span><br><span class="line">    &#125;) </span><br><span class="line">    ]</span><br><span class="line">&#125;)</span><br><span class="line"><span class="number">2</span>-生产配置文件 webpack.pro.js <span class="comment">//同上面一样</span></span><br><span class="line"><span class="number">3</span>-这个时候我们就可以再json文件里写脚本了 </span><br><span class="line"><span class="string">&quot;scripts&quot;</span>:&#123;</span><br><span class="line">    <span class="string">&quot;build&quot;</span>:<span class="string">&quot;webpack --config webpack.pro.js&quot;</span></span><br><span class="line">    <span class="string">&quot;dev&quot;</span>:<span class="string">&quot;webpack --config webpack.dev.js&quot;</span></span><br><span class="line">  &#125;,</span><br></pre></td></tr></table></figure>
<h2 id="18-优化"><a href="#18-优化" class="headerlink" title="18.优化"></a>18.优化</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>noParse 我们webpack再打包的时候 需要解析主包 和别的包之间的依赖关系 但是我们有的包只有自己并没有依赖别的包 比如jquery 这个时候就需要用到noParse</span><br><span class="line"><span class="built_in">module</span>:&#123;</span><br><span class="line">    noParse:<span class="regexp">/jquery/</span>, <span class="comment">//不去解析jquery的依赖关系</span></span><br><span class="line">    rules:[</span><br><span class="line">        &#123;&#125;</span><br><span class="line">    ]</span><br><span class="line">&#125;</span><br><span class="line"><span class="number">2.</span>IgnorPlugin  是webpack的一个内置插件 比如我们再引入包的时候 包和包之间会有依赖关系 有的时候有些依赖的包关系我们是用不到的 所以不需要他去打包 这个时候就需要用到IgnorPlugin 比如moment包 她有多语言设置 但我是们再打包的时候 会把这些多语言都打包进去 这个时候我们就需要用中文 而其他的不用 所以这个时候就要去用IgnorPlugin</span><br><span class="line"><span class="keyword">let</span> webpack = <span class="built_in">require</span>(<span class="string">&#x27;webpack&#x27;</span>) <span class="comment">//先引入webpack</span></span><br><span class="line">plugins:[</span><br><span class="line">    <span class="comment">//第一个参数是你包和包依赖的路径 第二个参数 是你要解除的依赖的入口包是哪个 都是正则表达式</span></span><br><span class="line">    <span class="keyword">new</span> webpack.IgnorPlugin(<span class="regexp">/\.\locale/</span>,<span class="regexp">/moment/</span>)</span><br><span class="line">]</span><br></pre></td></tr></table></figure>
<h2 id="19-dllPlugin-动态链接库"><a href="#19-dllPlugin-动态链接库" class="headerlink" title="19.dllPlugin 动态链接库"></a>19.dllPlugin 动态链接库</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>动态链接库的作用 比如我们的第三方模块 我们是不会更改的他原本的代码 比如我们的vue 但是我们每次打包的时候 这些都会再被重新打包 </span><br><span class="line"><span class="number">2.</span>这个时候我们就需要先将这些我们不更改的第三包先去打包 打包会生成一个动态链接库 然后我们在打包我们更改的代码的时候 如果涉及到我们这些已经打包完毕的第三方包 我们就去加载这个动态链接库</span><br><span class="line"><span class="number">3.</span>这个配置文件执行完毕之后会生成两个文件 一个是打包好的js文件 一个是manifest.json文件(动态链接清单)</span><br><span class="line">我们最后在打包别的文件的时候引入的就是这个manifest.json文件(动态链接清单)</span><br><span class="line"><span class="number">4.</span>我们需要在html文件 script标签 先引入一下我们打包好的文件</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//示例</span></span><br><span class="line"><span class="comment">//test.js文件中我们导出了一个字符串 然后去用webpack去打包</span></span><br><span class="line"><span class="built_in">module</span>.exports = <span class="string">&#x27;我是最新的测试&#x27;</span></span><br><span class="line"><span class="comment">//webpack.test.conf.js 打包文件</span></span><br><span class="line"><span class="keyword">const</span> path = <span class="built_in">require</span>(<span class="string">&#x27;path&#x27;</span>)</span><br><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">    mode:<span class="string">&#x27;development&#x27;</span>,</span><br><span class="line">    entry:<span class="string">&#x27;./src/test.js&#x27;</span>,</span><br><span class="line">    output:&#123;</span><br><span class="line">        filename:<span class="string">&#x27;[name][hash:9].js&#x27;</span>,</span><br><span class="line">        path:path.resolve(__dirname,<span class="string">&#x27;dist&#x27;</span>)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//打包好的文件 是有返回值的 但是没有变量接收 </span></span><br><span class="line"> (<span class="function"><span class="keyword">function</span>(<span class="params">modules</span>) </span>&#123; </span><br><span class="line"> 	<span class="keyword">var</span> installedModules = &#123;&#125;;</span><br><span class="line"> 	<span class="function"><span class="keyword">function</span> <span class="title">__webpack_require__</span>(<span class="params">moduleId</span>) </span>&#123;</span><br><span class="line"> 		<span class="keyword">if</span>(installedModules[moduleId]) &#123;</span><br><span class="line"> 			<span class="keyword">return</span> installedModules[moduleId].exports;</span><br><span class="line"> 		&#125;</span><br><span class="line"> 		<span class="keyword">var</span> <span class="built_in">module</span> = installedModules[moduleId] = &#123;</span><br><span class="line"> 			i: moduleId,</span><br><span class="line"> 			l: <span class="literal">false</span>,</span><br><span class="line"> 			<span class="built_in">exports</span>: &#123;&#125;</span><br><span class="line"> 		&#125;;</span><br><span class="line"></span><br><span class="line"> 		modules[moduleId].call(<span class="built_in">module</span>.exports, <span class="built_in">module</span>, <span class="built_in">module</span>.exports, __webpack_require__);</span><br><span class="line"> 		<span class="built_in">module</span>.l = <span class="literal">true</span>;</span><br><span class="line"> 		<span class="keyword">return</span> <span class="built_in">module</span>.exports;</span><br><span class="line"> 	&#125;</span><br><span class="line"></span><br><span class="line"> 	__webpack_require__.m = modules;</span><br><span class="line"> 	__webpack_require__.c = installedModules;</span><br><span class="line"> 	__webpack_require__.d = <span class="function"><span class="keyword">function</span>(<span class="params"><span class="built_in">exports</span>, name, getter</span>) </span>&#123;</span><br><span class="line"> 		<span class="keyword">if</span>(!__webpack_require__.o(<span class="built_in">exports</span>, name)) &#123;</span><br><span class="line"> 			<span class="built_in">Object</span>.defineProperty(<span class="built_in">exports</span>, name, &#123; <span class="attr">enumerable</span>: <span class="literal">true</span>, <span class="attr">get</span>: getter &#125;);</span><br><span class="line"> 		&#125;</span><br><span class="line"> 	&#125;;</span><br><span class="line"> 	__webpack_require__.r = <span class="function"><span class="keyword">function</span>(<span class="params"><span class="built_in">exports</span></span>) </span>&#123;</span><br><span class="line"> 		<span class="keyword">if</span>(<span class="keyword">typeof</span> <span class="built_in">Symbol</span> !== <span class="string">&#x27;undefined&#x27;</span> &amp;&amp; <span class="built_in">Symbol</span>.toStringTag) &#123;</span><br><span class="line"> 			<span class="built_in">Object</span>.defineProperty(<span class="built_in">exports</span>, <span class="built_in">Symbol</span>.toStringTag, &#123; <span class="attr">value</span>: <span class="string">&#x27;Module&#x27;</span> &#125;);</span><br><span class="line"> 		&#125;</span><br><span class="line"> 		<span class="built_in">Object</span>.defineProperty(<span class="built_in">exports</span>, <span class="string">&#x27;__esModule&#x27;</span>, &#123; <span class="attr">value</span>: <span class="literal">true</span> &#125;);</span><br><span class="line"> 	&#125;;</span><br><span class="line"> 	__webpack_require__.t = <span class="function"><span class="keyword">function</span>(<span class="params">value, mode</span>) </span>&#123;</span><br><span class="line"> 		<span class="keyword">if</span>(mode &amp; <span class="number">1</span>) value = __webpack_require__(value);</span><br><span class="line"> 		<span class="keyword">if</span>(mode &amp; <span class="number">8</span>) <span class="keyword">return</span> value;</span><br><span class="line"> 		<span class="keyword">if</span>((mode &amp; <span class="number">4</span>) &amp;&amp; <span class="keyword">typeof</span> value === <span class="string">&#x27;object&#x27;</span> &amp;&amp; value &amp;&amp; value.__esModule) <span class="keyword">return</span> value;</span><br><span class="line"> 		<span class="keyword">var</span> ns = <span class="built_in">Object</span>.create(<span class="literal">null</span>);</span><br><span class="line"> 		__webpack_require__.r(ns);</span><br><span class="line"> 		<span class="built_in">Object</span>.defineProperty(ns, <span class="string">&#x27;default&#x27;</span>, &#123; <span class="attr">enumerable</span>: <span class="literal">true</span>, <span class="attr">value</span>: value &#125;);</span><br><span class="line"> 		<span class="keyword">if</span>(mode &amp; <span class="number">2</span> &amp;&amp; <span class="keyword">typeof</span> value != <span class="string">&#x27;string&#x27;</span>) <span class="keyword">for</span>(<span class="keyword">var</span> key <span class="keyword">in</span> value) __webpack_require__.d(ns, key, <span class="function"><span class="keyword">function</span>(<span class="params">key</span>) </span>&#123; <span class="keyword">return</span> value[key]; &#125;.bind(<span class="literal">null</span>, key));</span><br><span class="line"> 		<span class="keyword">return</span> ns;</span><br><span class="line"> 	&#125;;</span><br><span class="line"> 	__webpack_require__.n = <span class="function"><span class="keyword">function</span>(<span class="params"><span class="built_in">module</span></span>) </span>&#123;</span><br><span class="line"> 		<span class="keyword">var</span> getter = <span class="built_in">module</span> &amp;&amp; <span class="built_in">module</span>.__esModule ?</span><br><span class="line"> 			<span class="function"><span class="keyword">function</span> <span class="title">getDefault</span>(<span class="params"></span>) </span>&#123; <span class="keyword">return</span> <span class="built_in">module</span>[<span class="string">&#x27;default&#x27;</span>]; &#125; :</span><br><span class="line"> 			<span class="function"><span class="keyword">function</span> <span class="title">getModuleExports</span>(<span class="params"></span>) </span>&#123; <span class="keyword">return</span> <span class="built_in">module</span>; &#125;;</span><br><span class="line"> 		__webpack_require__.d(getter, <span class="string">&#x27;a&#x27;</span>, getter);</span><br><span class="line"> 		<span class="keyword">return</span> getter;</span><br><span class="line"> 	&#125;;</span><br><span class="line"> 	__webpack_require__.o = <span class="function"><span class="keyword">function</span>(<span class="params">object, property</span>) </span>&#123; <span class="keyword">return</span> <span class="built_in">Object</span>.prototype.hasOwnProperty.call(object, property); &#125;;</span><br><span class="line"> 	__webpack_require__.p = <span class="string">&quot;&quot;</span>;</span><br><span class="line">     <span class="comment">//这里是由返回值的 </span></span><br><span class="line"> 	<span class="keyword">return</span> __webpack_require__(__webpack_require__.s = <span class="string">&quot;./src/test.js&quot;</span>);</span><br><span class="line"> &#125;)</span><br><span class="line"> (&#123;</span><br><span class="line"></span><br><span class="line"><span class="comment">/***/</span> <span class="string">&quot;./src/test.js&quot;</span>:</span><br><span class="line"><span class="comment">/*!*********************!*\</span></span><br><span class="line"><span class="comment">  !*** ./src/test.js ***!</span></span><br><span class="line"><span class="comment">  \*********************/</span></span><br><span class="line"><span class="comment">/*! no static exports found */</span></span><br><span class="line"><span class="comment">/***/</span> (<span class="function"><span class="keyword">function</span>(<span class="params"><span class="built_in">module</span>, <span class="built_in">exports</span></span>) </span>&#123;</span><br><span class="line"></span><br><span class="line"><span class="built_in">eval</span>(<span class="string">&quot;module.exports = &#x27;我是最新的测试&#x27;\n\n//# sourceURL=webpack:///./src/test.js?&quot;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">/***/</span> &#125;)</span><br><span class="line"></span><br><span class="line"> &#125;);</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//以上的打包结果我们需要手动去创建变量去接收 但是这样做太麻烦 所以 引出了library选项</span></span><br><span class="line"><span class="built_in">module</span>.exports = <span class="string">&#x27;我是最新的测试&#x27;</span></span><br><span class="line"><span class="comment">//webpack.test.conf.js 打包文件</span></span><br><span class="line"><span class="keyword">const</span> path = <span class="built_in">require</span>(<span class="string">&#x27;path&#x27;</span>)</span><br><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">    mode:<span class="string">&#x27;development&#x27;</span>,</span><br><span class="line">    entry:<span class="string">&#x27;./src/test.js&#x27;</span>,</span><br><span class="line">    output:&#123;</span><br><span class="line">        filename:<span class="string">&#x27;[name][hash:9].js&#x27;</span>,</span><br><span class="line">        path:path.resolve(__dirname,<span class="string">&#x27;dist&#x27;</span>),</span><br><span class="line">        <span class="comment">//指定你打包好的结果用什么去接收 默认是var</span></span><br><span class="line">        library:<span class="string">&#x27;_dll[name]&#x27;</span>,</span><br><span class="line">        <span class="comment">//也可以采用别的接收规范 比如 amd commonjs</span></span><br><span class="line">        libraryTarget:<span class="string">&#x27;commonjs&#x27;</span></span><br><span class="line">    &#125;,</span><br><span class="line">    plugin:[</span><br><span class="line">        <span class="comment">//这个就是生成了我们刚才说的目录 这些目录记载了我们刚刚那些生成的变量 根据根据变量去找我们已经提前打好的包模块</span></span><br><span class="line">        <span class="keyword">new</span> webpack.DllPlugin(&#123;</span><br><span class="line">        name:<span class="string">&#x27;_dll[name]&#x27;</span>, <span class="comment">//注意这里要和我们上面的library同名否则找不到</span></span><br><span class="line">        path:path.resolve(__dirname,<span class="string">&#x27;dist&#x27;</span>,<span class="string">&#x27;manifest.json&#x27;</span>) <span class="comment">//目录生成到哪里</span></span><br><span class="line">    &#125;)</span><br><span class="line">    ]</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//这里为什么要用一个变量接收 因为我们最后还有生成一个目录 目录里会有这些代码的变量 然后在加载的时候根据这些目录里的变量 去找我们提前打包好的文件</span></span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//以上我们的包也打好了 目录页生成了 然后我们就需要在正式的打包文件中去引入这个目录了</span></span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//以下是我们需要先打包的第三方模块 或者 需要生成动态链接清单的第三方包</span></span><br><span class="line"><span class="keyword">const</span> webpack = <span class="built_in">require</span>(<span class="string">&quot;webpack&quot;</span>);</span><br><span class="line"><span class="keyword">const</span> path = <span class="built_in">require</span>(<span class="string">&#x27;path&#x27;</span>);</span><br><span class="line"><span class="keyword">const</span> &#123;CleanWebpackPlugin&#125; = <span class="built_in">require</span>(<span class="string">&quot;clean-webpack-plugin&quot;</span>);</span><br><span class="line"><span class="keyword">const</span> dllPath = path.resolve(__dirname, <span class="string">&quot;../static/cdn/dll&quot;</span>); <span class="comment">// dll文件存放的目录</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">  entry: &#123;</span><br><span class="line">    <span class="comment">// 把 vue 相关模块的放到一个单独的动态链接库</span></span><br><span class="line">    vendor: [<span class="string">&quot;babel-polyfill&quot;</span>, <span class="string">&quot;vue&quot;</span>, <span class="string">&quot;vue-router&quot;</span>, <span class="string">&quot;vuex&quot;</span>, <span class="string">&quot;axios&quot;</span>, <span class="string">&quot;element-ui&quot;</span>, <span class="string">&quot;@smallwei/avue&quot;</span>]</span><br><span class="line">  &#125;,</span><br><span class="line">  output: &#123;</span><br><span class="line">    <span class="comment">// 文件名称</span></span><br><span class="line">    filename: <span class="string">&#x27;[name].dll.js&#x27;</span>,</span><br><span class="line">    path: dllPath,</span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">     存放相关的dll文件的全局变量名称，比如对于jquery来说的话就是 _dll_jquery, 在前面加 _dll</span></span><br><span class="line"><span class="comment">     是为了防止全局变量冲突。</span></span><br><span class="line"><span class="comment">    */</span></span><br><span class="line">    library: <span class="string">&#x27;[name]_library&#x27;</span>,</span><br><span class="line">    libraryTarget: <span class="string">&#x27;umd&#x27;</span></span><br><span class="line">  &#125;,</span><br><span class="line">  plugins: [</span><br><span class="line">    <span class="keyword">new</span> CleanWebpackPlugin(),</span><br><span class="line">    <span class="keyword">new</span> webpack.DllPlugin(&#123;</span><br><span class="line">      name: <span class="string">&#x27;[name]_library&#x27;</span>,</span><br><span class="line">      <span class="comment">// manifest.json 描述动态链接库包含了哪些内容</span></span><br><span class="line">      path: path.join(__dirname, <span class="string">&quot;./&quot;</span>, <span class="string">&quot;[name].dll.manifest.json&quot;</span>)</span><br><span class="line">    &#125;),</span><br><span class="line">  ],</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//上面的代码已经生成了动态链接清单 下一步我们在打包我们的别的代码的时候就需要引入这个manifest.json文件(动态链接清单) 下面是配置 引入之前 你的html模板需要引入一下 提前打包好的第三方文件</span></span><br><span class="line"><span class="keyword">let</span> webpack = <span class="built_in">require</span>(<span class="string">&#x27;webpack&#x27;</span>)</span><br><span class="line">plugins:[</span><br><span class="line">    <span class="keyword">new</span> webpack.DllRefencePlugin(&#123;</span><br><span class="line">        manifest:path.resolve(__dirname,<span class="string">&#x27;dist&#x27;</span>,<span class="string">&#x27;manifest.json&#x27;</span>)</span><br><span class="line">    &#125;)</span><br><span class="line">]</span><br></pre></td></tr></table></figure>
<h2 id="20-多线程打包-happypack"><a href="#20-多线程打包-happypack" class="headerlink" title="20.多线程打包 happypack"></a>20.多线程打包 happypack</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>主要应用 happypack</span><br><span class="line"><span class="number">2.</span>比如我们以前在打包js文件的时候用的babel相关的loader 这个时候我们要把打包JS的loader换成happypack 让happypack去多线程执行打包js的一些laoder 比如babel</span><br><span class="line"><span class="number">3.</span>happypack的use 必须是个数组</span><br><span class="line"><span class="number">4.</span>如果处理别的loader也需要多线程 比如处理css的loader 那么就要重新<span class="keyword">new</span> Happypack一下 写法跟下面一样</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> path = <span class="built_in">require</span>(<span class="string">&#x27;path&#x27;</span>)</span><br><span class="line"><span class="comment">//因为这里大写了 所以这是个构造函数也是es里的类 所以我们需要new出来</span></span><br><span class="line"><span class="keyword">let</span> HtmlWebpackPlugin = <span class="built_in">require</span>(<span class="string">&#x27;html-webpack-plugin&#x27;</span>)</span><br><span class="line"><span class="keyword">let</span> HappyPack = <span class="built_in">require</span>(<span class="string">&#x27;HappyPack&#x27;</span>)</span><br><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">    mode:<span class="string">&#x27;development&#x27;</span></span><br><span class="line">    extry:<span class="string">&#x27;./src/index.js&#x27;</span></span><br><span class="line">    output:&#123;</span><br><span class="line">    filename:<span class="string">&#x27;bundle.js&#x27;</span></span><br><span class="line">    path:path.resolve(__dirname,<span class="string">&#x27;build&#x27;</span>) </span><br><span class="line">	&#125;</span><br><span class="line">    <span class="built_in">module</span>:&#123;</span><br><span class="line">        rules:[</span><br><span class="line">            &#123;</span><br><span class="line">             test:<span class="string">&#x27;/\.js$/&#x27;</span> </span><br><span class="line">             <span class="comment">//改为用happypack去执行babel的loader 要给一个ID </span></span><br><span class="line">             use:<span class="string">&#x27;Happypack/laoder?id=js&#x27;</span></span><br><span class="line">            &#125;,</span><br><span class="line">            &#123;</span><br><span class="line">               test:<span class="string">&#x27;/\.css$/&#x27;</span>,</span><br><span class="line">               use:<span class="string">&#x27;Happypack/laoder?id=css&#x27;</span></span><br><span class="line">            &#125;</span><br><span class="line">        ]</span><br><span class="line">    &#125;,</span><br><span class="line">    plguins:[</span><br><span class="line">        <span class="keyword">new</span> Happypack(&#123;</span><br><span class="line">            id:<span class="string">&#x27;css&#x27;</span>,</span><br><span class="line">            use:[</span><br><span class="line">                loader:[<span class="string">&#x27;style-loader&#x27;</span>,<span class="string">&#x27;css-loader&#x27;</span>]</span><br><span class="line">            ]</span><br><span class="line">        &#125;)</span><br><span class="line">        <span class="comment">//这里加载happypack 去执行你上面写好ID的那个loader</span></span><br><span class="line">        <span class="keyword">new</span> Happypack(&#123;</span><br><span class="line">            id:<span class="string">&#x27;js&#x27;</span>,</span><br><span class="line">            use:[</span><br><span class="line">             	loader:<span class="string">&#x27;babel-loader&#x27;</span>,</span><br><span class="line">             	options:&#123;</span><br><span class="line">             		presets:[<span class="string">&#x27;@babel/preset-env&#x27;</span>],</span><br><span class="line">            		plugins:[<span class="string">&#x27;@babel/plugin-proposal-class-properties&#x27;</span>,<span class="string">&#x27;@balbel/plugin-transform-runtime&#x27;</span></span><br><span class="line">            &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            &#125;, </span><br><span class="line">        ]</span><br><span class="line">            ]</span><br><span class="line">        &#125;)</span><br><span class="line">        <span class="keyword">new</span> HtmlWebpackPlugin(&#123;</span><br><span class="line">            template:<span class="string">&#x27;./src/index.html&#x27;</span><span class="comment">//你模板的路径</span></span><br><span class="line">            filename:<span class="string">&#x27;index.html&#x27;</span><span class="comment">//你希望再内存路径中访问的文件地址</span></span><br><span class="line">            minify:&#123;</span><br><span class="line">            	removeAttributeQuotes:<span class="literal">true</span>,</span><br><span class="line">         		collapseWhitespace:<span class="literal">true</span></span><br><span class="line">        &#125;</span><br><span class="line">        &#125;)</span><br><span class="line">    ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="21-webpack自带优化"><a href="#21-webpack自带优化" class="headerlink" title="21.webpack自带优化"></a>21.webpack自带优化</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>比如我们在一个js文件里写了若干个方法 然后再另外一个js文件里用<span class="keyword">import</span>语法引用    而且只使用了其中的一个方法 而其他的方法没有用到 这个时候你再打包的时候 webpack会自动指帮你打包你使用的方法而没用的代码自动删除 叫tree-shaking(树摇摆) 注意 必须是<span class="keyword">import</span>语法 (再生产环境下才会生效)</span><br><span class="line"><span class="number">2.</span>我们再使用<span class="built_in">require</span>导入模块的时候 会自动把我们导出的方法挂在到<span class="keyword">default</span>上面</span><br></pre></td></tr></table></figure>
<h2 id="22-抽取公共代码"><a href="#22-抽取公共代码" class="headerlink" title="22.抽取公共代码"></a>22.抽取公共代码</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>针对于多页面打包</span><br><span class="line"><span class="number">2.</span>比如我们有两个入口文件需要打包 但是这两个文件都引入了相同的模块 这个时候我们就需要把相同的公共模块抽离出来打包</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">    optimization:&#123;</span><br><span class="line">        <span class="comment">//分割代码块配置</span></span><br><span class="line">        splitChunks:&#123;</span><br><span class="line">            <span class="comment">//缓存组</span></span><br><span class="line">            cacheGroups:&#123;</span><br><span class="line">                common:&#123;</span><br><span class="line">                    chunks:<span class="string">&#x27;initial&#x27;</span>,</span><br><span class="line">                    <span class="comment">//超过多少大小的需要缓存</span></span><br><span class="line">                    minSize:<span class="number">0</span>,</span><br><span class="line">                    <span class="comment">//被使用了多少次的需要抽离</span></span><br><span class="line">                    minChunks:<span class="number">2</span></span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="23-懒加载"><a href="#23-懒加载" class="headerlink" title="23.懒加载"></a>23.懒加载</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>当我们点击某个按钮或者开关的时候才去加载一段js代码</span><br><span class="line"><span class="number">2.</span>原理 就是通过<span class="keyword">import</span> 语法 回调是一个promise</span><br><span class="line"><span class="number">3.</span>需要安装对应的plugin才可以实现</span><br><span class="line"><span class="keyword">import</span>(<span class="string">&#x27;./a.js&#x27;</span>).then(<span class="function"><span class="params">data</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(data.default)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
</article><div class="post-copyright"><div class="post-copyright__title"><span class="post-copyright-info"><h>Webpack4.0</h></span></div><div class="post-copyright__type"><span class="post-copyright-info"><a href="http://example.com/2019/03/04/webpack4.0/">http://example.com/2019/03/04/webpack4.0/</a></span></div><div class="post-copyright-m"><div class="post-copyright-m-info"><div class="post-copyright-a"><h>作者</h><div class="post-copyright-cc-info"><h>北城韩雨</h></div></div><div class="post-copyright-c"><h>发布于</h><div class="post-copyright-cc-info"><h>2019-03-04</h></div></div><div class="post-copyright-u"><h>更新于</h><div class="post-copyright-cc-info"><h>2019-06-03</h></div></div><div class="post-copyright-c"><h>许可协议</h><div class="post-copyright-cc-info"><a class="icon" rel="noopener" target="_blank" title="Creative Commons" href="https://creativecommons.org/"><i class="fab fa-creative-commons"></i></a><a rel="noopener" target="_blank" title="CC BY-NC-SA 4.0" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a></div></div></div></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/Webpack/">Webpack</a></div><div class="post_share"></div></div><link rel="stylesheet" href="/" media="defer" onload="this.media='all'"/><div class="post-reward"><button class="tip-button reward-button"><span class="tip-button__text">打赏</span><div class="coin-wrapper"><div class="coin"><div class="coin__middle"></div><div class="coin__back"></div><div class="coin__front"></div></div></div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="/img/weixin.jpg" target="_blank"><img class="post-qr-code-img" src="/img/weixin.jpg" alt="微信"/></a><div class="post-qr-code-desc">微信</div></li><li class="reward-item"><a href="/img/taobao.jpg" target="_blank"><img class="post-qr-code-img" src="/img/taobao.jpg" alt="支付宝"/></a><div class="post-qr-code-desc">支付宝</div></li></ul></div></button></div><script defer="defer" src="/"></script><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2019/03/05/webpack%E5%AE%9E%E6%88%98%E9%85%8D%E7%BD%AE%E8%A7%A3%E6%9E%90/"><img class="prev-cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2Faf41dabad727ba80c9ca8a2bfbd22a58%2F00004.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530659&amp;t=bbcfe1e86f393eecbb0722db1b92e475" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">Webpack实战配置解析</div></div></a></div><div class="next-post pull-right"><a href="/2019/03/03/webpack%E5%89%8D%E7%BD%AE%E6%A6%82%E5%BF%B5/"><img class="next-cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-12-05%2F66b696ea615f3567a32b65f93611a814%2F00004.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530687&amp;t=c2e3c97de6dbdc29e77593d5f4dc7fb5" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">Webpack 前置概念</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span> 相关推荐</span></div><div class="relatedPosts-list"><div><a href="/2019/03/05/webpack实战配置解析/" title="Webpack实战配置解析"><img class="cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2Faf41dabad727ba80c9ca8a2bfbd22a58%2F00004.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&refer=http%3A%2F%2Fpic.vjshi.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629530659&t=bbcfe1e86f393eecbb0722db1b92e475" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2019-03-05</div><div class="title">Webpack实战配置解析</div></div></a></div><div><a href="/2019/03/03/webpack前置概念/" title="Webpack 前置概念"><img class="cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-12-05%2F66b696ea615f3567a32b65f93611a814%2F00004.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&refer=http%3A%2F%2Fpic.vjshi.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1629530687&t=c2e3c97de6dbdc29e77593d5f4dc7fb5" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2019-03-03</div><div class="title">Webpack 前置概念</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/img/my.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">北城韩雨</div><div class="author-info__description">一个前端界的小学生</div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">65</div></a></div><div class="card-info-data-item is-center"><a href="/tags/"><div class="headline">标签</div><div class="length-num">23</div></a></div><div class="card-info-data-item is-center"><a href="/categories/"><div class="headline">分类</div><div class="length-num">21</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/wazer1987"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="/wangzhengvslike@163.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content"><p>感谢访问本站，如喜欢请收藏。本站主要分享前端知识，立志成为资深前端工程师，但目前是一个前端界的小学生 若喜欢可以打赏请博主喝一杯冰阔落</p> <p>另外请大家多多支持淼哥的开源项目</p> <p>https://github.com/flipped-aurora/gin-vue-admin</p> <p>🌟🌟欢迎大家start 🌟🌟</p> <p>欢迎加入博主的前端技术交流群</p> <p style="background:rgba(255,255,255,1);margin:0"> <img src="/img/code.png"></img> </p></div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#webpack4-0"><span class="toc-text">webpack4.0</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-%E5%AE%89%E8%A3%85"><span class="toc-text">1.安装</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-%E6%89%93%E5%8C%85"><span class="toc-text">2.打包</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-%E6%89%8B%E5%8A%A8%E9%85%8D%E7%BD%AEwebpakc"><span class="toc-text">3.手动配置webpakc</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-webpack-config-js%E5%9F%BA%E6%9C%AC%E9%85%8D%E7%BD%AE"><span class="toc-text">4.webpack.config.js基本配置</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-%E6%9B%B4%E6%94%B9%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E5%90%8D%E7%A7%B0"><span class="toc-text">4.更改配置文件名称</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5-%E5%90%AF%E5%8A%A8%E6%9C%8D%E5%8A%A1"><span class="toc-text">5.启动服务</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#6-%E6%A0%B7%E5%BC%8F%E5%A4%84%E7%90%86"><span class="toc-text">6.样式处理</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#7-%E5%A4%84%E7%90%86js%E6%A8%A1%E5%9D%97"><span class="toc-text">7.处理js模块</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#8-%E8%AF%AD%E6%B3%95%E6%A0%A1%E9%AA%8C"><span class="toc-text">8.语法校验</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#9-%E5%85%A8%E5%B1%80%E5%8F%98%E9%87%8F%E5%BC%95%E5%85%A5"><span class="toc-text">9.全局变量引入</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#10-%E5%9B%BE%E7%89%87%E5%BC%95%E5%85%A5%E7%9A%84%E9%97%AE%E9%A2%98"><span class="toc-text">10.图片引入的问题</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#10-loader%E7%9A%84%E4%BD%BF%E7%94%A8"><span class="toc-text">10.loader的使用</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#11-%E5%88%9B%E5%BB%BA%E5%88%86%E7%B1%BB"><span class="toc-text">11.创建分类</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#12-%E6%89%93%E5%8C%85%E5%A4%9A%E9%A1%B5%E9%9D%A2%E5%BA%94%E7%94%A8"><span class="toc-text">12.打包多页面应用</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#13-%E9%85%8D%E7%BD%AEsource-map"><span class="toc-text">13.配置source-map</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#14-webpack%E8%B7%A8%E5%9F%9F"><span class="toc-text">14.webpack跨域</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#15-resolve%E5%B1%9E%E6%80%A7%E7%9A%84%E9%85%8D%E7%BD%AE"><span class="toc-text">15.resolve属性的配置</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#16-%E5%AE%9A%E4%B9%89%E7%8E%AF%E5%A2%83%E5%8F%98%E9%87%8F"><span class="toc-text">16.定义环境变量</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#17-%E5%8C%BA%E5%88%86%E4%B8%8D%E5%90%8C%E7%8E%AF%E5%A2%83"><span class="toc-text">17.区分不同环境</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#18-%E4%BC%98%E5%8C%96"><span class="toc-text">18.优化</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#19-dllPlugin-%E5%8A%A8%E6%80%81%E9%93%BE%E6%8E%A5%E5%BA%93"><span class="toc-text">19.dllPlugin 动态链接库</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#20-%E5%A4%9A%E7%BA%BF%E7%A8%8B%E6%89%93%E5%8C%85-happypack"><span class="toc-text">20.多线程打包 happypack</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#21-webpack%E8%87%AA%E5%B8%A6%E4%BC%98%E5%8C%96"><span class="toc-text">21.webpack自带优化</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#22-%E6%8A%BD%E5%8F%96%E5%85%AC%E5%85%B1%E4%BB%A3%E7%A0%81"><span class="toc-text">22.抽取公共代码</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#23-%E6%87%92%E5%8A%A0%E8%BD%BD"><span class="toc-text">23.懒加载</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2021/04/16/gin-vue-admin/" title="Gin-Vue-Admin"><img src="/img/gin-vue-admin.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Gin-Vue-Admin"/></a><div class="content"><a class="title" href="/2021/04/16/gin-vue-admin/" title="Gin-Vue-Admin">Gin-Vue-Admin</a><time datetime="2021-04-15T16:00:00.000Z" title="发表于 2021-04-16 00:00:00">2021-04-16</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/01/08/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%BB%84%E4%BB%B6%E7%9A%84%E6%B8%B2%E6%9F%9310/" title="Vue源码开发-组件的渲染10"><img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2F3eca5394963b50e37b7be2ba320029ec%2F00003.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530644&amp;t=10679b5732c52e39eeb588040240a790" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Vue源码开发-组件的渲染10"/></a><div class="content"><a class="title" href="/2021/01/08/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%BB%84%E4%BB%B6%E7%9A%84%E6%B8%B2%E6%9F%9310/" title="Vue源码开发-组件的渲染10">Vue源码开发-组件的渲染10</a><time datetime="2021-01-07T16:00:00.000Z" title="发表于 2021-01-08 00:00:00">2021-01-08</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/01/07/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%BB%84%E4%BB%B6%E7%9A%84%E5%90%88%E5%B9%B6%E7%AD%96%E7%95%A509/" title="Vue源码开发-组件合并策略09"><img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com%2F20191017%2F4cfc341591d242327b67e9ceb6953612_00002.jpg&amp;refer=http%3A%2F%2Fcz-video-photo.oss-cn-beijing.aliyuncs.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530702&amp;t=80943e12d5239f63beafd6d1c540855f" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Vue源码开发-组件合并策略09"/></a><div class="content"><a class="title" href="/2021/01/07/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%BB%84%E4%BB%B6%E7%9A%84%E5%90%88%E5%B9%B6%E7%AD%96%E7%95%A509/" title="Vue源码开发-组件合并策略09">Vue源码开发-组件合并策略09</a><time datetime="2021-01-06T16:00:00.000Z" title="发表于 2021-01-07 00:00:00">2021-01-07</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/01/06/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E6%95%B0%E7%BB%84%E7%9A%84%E4%BE%9D%E8%B5%96%E6%94%B6%E9%9B%8608/" title="Vue源码开发-数组的依赖收集08"><img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2F3eca5394963b50e37b7be2ba320029ec%2F00003.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530644&amp;t=10679b5732c52e39eeb588040240a790" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Vue源码开发-数组的依赖收集08"/></a><div class="content"><a class="title" href="/2021/01/06/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E6%95%B0%E7%BB%84%E7%9A%84%E4%BE%9D%E8%B5%96%E6%94%B6%E9%9B%8608/" title="Vue源码开发-数组的依赖收集08">Vue源码开发-数组的依赖收集08</a><time datetime="2021-01-05T16:00:00.000Z" title="发表于 2021-01-06 00:00:00">2021-01-06</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/01/06/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F%E5%90%88%E5%B9%B607/" title="Vue源码开发-生命周期合并07"><img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.vjshi.com%2F2018-07-17%2Faf41dabad727ba80c9ca8a2bfbd22a58%2F00004.jpg%3Fx-oss-process%3Dstyle%2Fwatermark&amp;refer=http%3A%2F%2Fpic.vjshi.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1629530659&amp;t=bbcfe1e86f393eecbb0722db1b92e475" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Vue源码开发-生命周期合并07"/></a><div class="content"><a class="title" href="/2021/01/06/Vue%E6%BA%90%E7%A0%81%E5%BC%80%E5%8F%91%E4%B9%8B%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F%E5%90%88%E5%B9%B607/" title="Vue源码开发-生命周期合并07">Vue源码开发-生命周期合并07</a><time datetime="2021-01-05T16:00:00.000Z" title="发表于 2021-01-06 00:00:00">2021-01-06</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright"><span>&copy;2017 - 2021</span><svg style="width:2em; height:2em;position: relative;top: 10px;" aria-hidden="true"><use xlink:href="#icon-Butterfly"></use></svg><span>北城韩雨</span></div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div class="footer_custom_text"><p><a style="margin-inline:5px" target="_blank" href="https://hexo.io/"><img src="https://img.shields.io/badge/Frame-Hexo-blue?style=flat&logo=hexo" title="博客框架为Hexo"></a><a style="margin-inline:5px" target="_blank" href="https://butterfly.js.org/"><img src="https://img.shields.io/badge/Theme-Butterfly-6513df?style=flat&logo=bitdefender" title="主题采用butterfly"></a><a style="margin-inline:5px" target="_blank" href="https://www.jsdelivr.com/"><img src="https://img.shields.io/badge/CDN-jsDelivr-orange?style=flat&logo=jsDelivr" title="本站使用JsDelivr为静态资源提供CDN加速"></a></p></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/medium-zoom/dist/medium-zoom.min.js"></script><div class="js-pjax"></div><script defer src="/live2d-widget/autoload.js"></script><script src="/js/coin.js"></script><script async src="//at.alicdn.com/t/font_2700632_3rtvhzzpl3h.js"></script><script src="/js/liuxing.js" data-pjax></script><canvas class="fireworks" mobile="false"></canvas><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/fireworks.min.js"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/activate-power-mode.min.js"></script><script>POWERMODE.colorful = true;
POWERMODE.shake = true;
POWERMODE.mobile = false;
document.body.addEventListener('input', POWERMODE);
</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><!-- hexo injector body_end start --><div class="js-pjax"><script async="async">var arr = document.getElementsByClassName('recent-post-item');
for(var i = 0;i<arr.length;i++){
    arr[i].classList.add('wow');
    arr[i].classList.add('animate__zoomIn');
    arr[i].setAttribute('data-wow-duration', '1s');
    arr[i].setAttribute('data-wow-delay', '0ms');
    arr[i].setAttribute('data-wow-offset', '100');
    arr[i].setAttribute('data-wow-iteration', '1');
  }</script><script async="async">var arr = document.getElementsByClassName('card-widget');
for(var i = 0;i<arr.length;i++){
    arr[i].classList.add('wow');
    arr[i].classList.add('animate__zoomIn');
    arr[i].setAttribute('data-wow-duration', '');
    arr[i].setAttribute('data-wow-delay', '');
    arr[i].setAttribute('data-wow-offset', '');
    arr[i].setAttribute('data-wow-iteration', '');
  }</script></div><script defer src="https://cdn.jsdelivr.net/gh/graingert/wow@1.3.0/dist/wow.min.js"></script><script defer src="https://cdn.jsdelivr.net/npm/hexo-butterfly-wowjs/lib/wow_init.min.js"></script><script async src="//at.alicdn.com/t/font_2032782_8d5kxvn09md.js"></script><!-- hexo injector body_end end --></body></html>